package com.ruoyi.gd.ywdwgdLog.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.gd.ywdwgd.domain.ZYwdwgd;
import com.ruoyi.gd.ywdwgdHistory.domain.vo.ZYwdwgdHistoryVo;
import com.ruoyi.gd.ywdwgdHistory.service.IZYwdwgdHistoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.ywdwgdLog.domain.bo.ZYwdwgdLogBo;
import com.ruoyi.gd.ywdwgdLog.domain.vo.ZYwdwgdLogVo;
import com.ruoyi.gd.ywdwgdLog.domain.ZYwdwgdLog;
import com.ruoyi.gd.ywdwgdLog.mapper.ZYwdwgdLogMapper;
import com.ruoyi.gd.ywdwgdLog.service.IZYwdwgdLogService;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业务单完工单日志Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-08
 */
@RequiredArgsConstructor
@Service
public class ZYwdwgdLogServiceImpl implements IZYwdwgdLogService {

    private final ZYwdwgdLogMapper baseMapper;

    @Resource
    private IZYwdwgdHistoryService historyService;

    /**
     * 查询业务单完工单日志
     */
    @Override
    public ZYwdwgdLogVo queryById(String id){
        ZYwdwgdLogVo zYwdwgdLogVo = baseMapper.selectVoById(id);
        // 如果添加前数据不为空则查询出来
        if (StringUtils.isNotEmpty(zYwdwgdLogVo.getFont())) {
            ZYwdwgdHistoryVo zYwdwgdHistoryVo = historyService.queryById(zYwdwgdLogVo.getFont());
            zYwdwgdLogVo.setFontForm(zYwdwgdHistoryVo);
        }
        // 如果添加后数据不为空则查询出来
        if (StringUtils.isNotEmpty(zYwdwgdLogVo.getLater())) {
            ZYwdwgdHistoryVo zYwdwgdHistoryVo = historyService.queryById(zYwdwgdLogVo.getLater());
            zYwdwgdLogVo.setLaterForm(zYwdwgdHistoryVo);
        }
        return zYwdwgdLogVo;
    }

    /**
     * 查询业务单完工单日志列表
     */
    @Override
    public TableDataInfo<ZYwdwgdLogVo> queryPageList(ZYwdwgdLogBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZYwdwgdLog> lqw = buildQueryWrapper(bo);
        Page<ZYwdwgdLogVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务单完工单日志列表
     */
    @Override
    public List<ZYwdwgdLogVo> queryList(ZYwdwgdLogBo bo) {
        LambdaQueryWrapper<ZYwdwgdLog> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZYwdwgdLog> buildQueryWrapper(ZYwdwgdLogBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZYwdwgdLog> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMsg()), ZYwdwgdLog::getMsg, bo.getMsg());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), ZYwdwgdLog::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getFont()), ZYwdwgdLog::getFont, bo.getFont());
        lqw.eq(StringUtils.isNotBlank(bo.getLater()), ZYwdwgdLog::getLater, bo.getLater());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateId()), ZYwdwgdLog::getCreateId, bo.getCreateId());
        lqw.like(StringUtils.isNotBlank(bo.getCreateBy()), ZYwdwgdLog::getCreateBy, bo.getCreateBy());
        if (bo.getParams()!=null && bo.getParams().size() > 0) {
            // 下场
            Object startTime = bo.getParams().get("startTime");
            Object endTime = bo.getParams().get("endTime");
            if (null != startTime && null != endTime) {
                lqw.ge(ZYwdwgdLog::getCreateTime, startTime);
                lqw.le(ZYwdwgdLog::getCreateTime, endTime);
            }
        }
        lqw.orderByDesc(ZYwdwgdLog::getCreateTime);
        return lqw;
    }

    /**
     * 新增业务单完工单日志
     */
    @Override
    public Boolean insertByBo(ZYwdwgdLogBo bo) {
        ZYwdwgdLog add = BeanUtil.toBean(bo, ZYwdwgdLog.class);
        validEntityBeforeSave(add);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateId(loginUser.getUserId()+"");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insert(ZYwdwgdLog add) {
        validEntityBeforeSave(add);
        LoginUser loginUser = LoginHelper.getLoginUser();
        add.setCreateId(loginUser.getUserId()+"");
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            add.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改业务单完工单日志
     */
    @Override
    public Boolean updateByBo(ZYwdwgdLogBo bo) {
        ZYwdwgdLog update = BeanUtil.toBean(bo, ZYwdwgdLog.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZYwdwgdLog entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务单完工单日志
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
